<?php
require_once 'BaseController.php';

/**
 * 【SP版】物件問い合わせ コントローラ
 */
class Mobileapi_BukkenentryController extends Mobileapi_BaseController
{

    /**
     * メインアクション
     */
    public function indexAction()
    {
        $params = $this->getParameters();
        $code = $params["BukkenCd"];
        $step = $params["Step"];

        if (empty($step))
        {
            $this->makeTemplateParams($code);
            // 店舗の表示順位を、代表物件以外はランダムにする。
            // 2店舗目が存在する場合、初期表示にチェックを状態になるようにする
            $this->bukkenShuffleAndDefaultChecked();
            $this->displayBody2('index');
        }
        else if ($step == 1)
        {
            $this->makeTemplateParams($code);

            if (empty($params['ShopCd0']))
            {
                $params['ShopCd0'] = $this->_data['buken']['tenpo1'][0]['T03_TENPO_CD'];
            }

            $this->makeTemplateParams1($params, $code);

            $model_cam = new Mobileapi_Model_BukkenCampaign();
            $campaigns = $model_cam->getNormalCampaigns();
            $this->_data['CAMPAIGNS'] = $campaigns;
            $this->displayBody2('entry');
        }
        else if ($step == 'complete')
        {
            if (sizeof($params['BukkenCds']) > 0)
            {
                $this->_data["MATOMERU"] = 1;
            }
            else
            {
                $this->_data["MATOMERU"] = 0;
            }

            // 検討リスト表示
            $this->loadData();

            $this->displayBody2('complete');
        }
    }

    public function postAction()
    {
        $params = $this->getParameters();
        $code = $params["BukkenCd"];
        $step = $params["Step"];

        $this->makeTemplateParams($code);
        $this->makeTemplateParams1($params, $code);
        $this->_data['params'] = $params;

        if ($step == 2)
        {
            $p_campaigns = $this->collectCampaignFromParams($params);
            $this->_data['p_campaigns'] = $p_campaigns;

            $model_cam = new Mobileapi_Model_BukkenCampaign();
            $temp = $model_cam->getNormalCampaigns();

            if ($this->checkParams($params, $code))
            {
                // 入力にエラーがある場合
                $campaigns = array();
                foreach ($temp as $k => $cam)
                {
                    if (in_array($cam['M94_CAMPAIGN_CD'], $p_campaigns))
                    {
                        $cam['name'] = 'campaign' . $k;
                        $cam['checked'] = 1;
                    }
                    $campaigns[] = $cam;
                }
                $this->_data['CAMPAIGNS'] = $campaigns;
                // エラー画面へ遷移
                $this->displayBody2('entry-error');
            }
            else
            {
                // 入力にエラーがない場合
                $campaigns = array();
                foreach ($temp as $k => $cam)
                {
                    if (in_array($cam['M94_CAMPAIGN_CD'], $p_campaigns))
                    {
                        $cam['name'] = 'campaign' . $k;
                        $campaigns[] = $cam;
                    }
                }
                $this->_data['CAMPAIGNS'] = $campaigns;
                // 確認画面へ遷移
                $this->displayBody2('confirm');
            }
        }
        else if ($step == 3)
        {
            $cparams = array();
            $cparams['Tel'] = $params['Tel'];
            $cparams['Mail'] = $params['Mail'];
            $cparams['vintGenruFlg'] = 95;
            $cparams['NameSei'] = $params['KanjiSei'];
            $cparams['NameMei'] = $params['KanjiMei'];

            // 20141211 S_Add
            $cparams['NameSeiKana'] = $params['KanaSei'];
            $cparams['NameMeiKana'] = $params['KanaMei'];

            $cparams['BukkenCd'] = implode(',', $params['bkcd']);
            // 20141211 E_Add

            // $list = $this->extractParams ( $params, 'ShopCd' );
            // $cparams ['BukkenCd'] = join ( ',', $list );

            $list = $this->extractParams($params, 'check');
            if (array_key_exists('check1', $params)) $cparams['Inquiry1'] = 1;
            if (array_key_exists('check2', $params)) $cparams['Inquiry3'] = 1;
            if (array_key_exists('check3', $params)) $cparams['Inquiry2'] = 1;
            if (array_key_exists('check4', $params)) $cparams['Inquiry4'] = 1;

            $list = $this->extractParams($params, 'campaign');
            $cparams['Comment'] = $params['shosai'];

            $list = $this->extractParams($params, 'campaign');
            $cparams['CampaignAgechau'] = join(';', $list);
            $cparams['TimeStr'] = date("Y/m/d H:i:s");

            $this->callInquiryApi('apibukken', $cparams);

            // $this->my_secure_redirect("sp/inquiry/bukkenentry/-/complete/?requestID={$this->_CV_data['CV_ToiawaseId']}&BukkenCd[]={$code}&revenue={$this->_CV_data['CV_TotalAmounts']}&ShopCds[]="
            // . join('&ShopCds[]=', $params['scds']));
            $this->my_secure_redirect("sp/inquiry/bukkenentry/-/complete/?requestID={$this->_CV_data['CV_ToiawaseId']}&BukkenCd[]={$code}");
        }
    }

    private function makeTemplateParams($code)
    {
        $model = new Rstdtl_Model_Bukendtl();

        $config = array();
        $params = array();

        $params['ID'] = $code;
        $params['buken_static'] = '1';
        $params['HeyaCd'] = $code;

        $data = $model->getBukenDtlListData($params);
        $model->convertData($config, $data, $params);

        $model_shop = new Mobileapi_Model_BukkenTenpo();
        $tenpo1 = $model_shop->getTenpoList($code);
        if (count($tenpo1) == 0)
        {
            $tenpo1 = $model_shop->getTenpoListHeya($code);
        }
        $data[0]['tenpo1'] = $tenpo1;

        // CALLNOTE
        foreach ($data[0]['tenpo1'] as $i => $tenpo)
        {
            $tempData = Apm_ShopUtil::getCallingData($tenpo, Apm_Config_Shop::FREE_CONNECTION_NO_1);
            $data[0]['tenpo1'][$i]['IS_FREE_CALL'] = $tempData['IS_FREE_CALL'];
            if ($tempData['IS_FREE_CALL'])
            {
                $data[0]['tenpo1'][$i]['FREE_CALLING'] = $tempData['TEL_01'];
                $data[0]['tenpo1'][$i]['CONVERT_CONNECTION_NO'] = Apm_Bukkenitems::GetBukkenWebCallingNo($tenpo, Apm_Config_Rstdtl::CAMPAIGN_PC);
            }
        }

        // Callクレヨン対応
        $smodel = new Shop_Model_Detail();
        foreach ($data[0]['tenpo1'] as $i => $tenpo)
        {
            $callCrayonNumber = $smodel->getCallCrayonNumber($this->_config['callcrayons']['api'], $this->_config['callcrayons']['service_id'], $data[0]['tenpo1'][$i]['T03_TENPO_CD'], '12');
            $data[0]['tenpo1'][$i]['CALL_CRAYON_NO'] = $callCrayonNumber;
            /*
             * if (! empty ( $data [0] ['tenpo1'] [$i] ['CALL_CRAYON_NO'] )) { // フリー番号ではない・・・ $data
             * [0] ['tenpo1'] [$i] ['hasFreeTel'] = false; $data [0] ['tenpo1'] [$i]
             * ['IS_FREE_CALL'] = false; }
             */
        }

        $this->_data['buken'] = $data[0];
    }

    private function makeTemplateParams1($params, $code)
    {
        $tenpo = $this->_data['buken']['tenpo1'];

        $shop = array();
        foreach ($params as $key => $val)
        {
            if (substr($key, 0, 6) == 'ShopCd')
            {
                $shop[$key] = array(
                    'code' => $val
                );
                foreach ($tenpo as $k1 => $v1)
                {
                    if ($v1['T03_TENPO_CD'] == $val)
                    {
                        $shop[$key]['name'] = $v1['T03_TENPO_NAME'];
                        $shop[$key]['heya'] = $v1['T03_HEYA_CD'];
                        break;
                    }
                }
            }
        }
        $this->_data['shop'] = $shop;

        $this->_data['shopCds'] = $this->extractParams($params, 'ShopCd');
    }

    private function checkParams($params, $code)
    {
        $error = 0;

        // 姓・名の全角文字チェック
        $key = array();
        $key[] = 'kanji_sei';
        $key[] = 'kanji_mei';
        foreach ($key as $k)
        {
            if ($this->checkZenkaku($params[$k]))
            {
                $this->_data['name_error'] = 1;
                $this->_data[$k . '_error'] = "error";
                $error = 1;
            }
        }
        // セイ・メイの全角カナ文字チェック
        $key = array();
        $key[] = 'kana_sei';
        $key[] = 'kana_mei';
        foreach ($key as $k)
        {
            if ($this->checkZenkakuKana($params[$k]))
            {
                $this->_data['name_error'] = "error";
                $this->_data[$k . '_error'] = "error";
                $error = 1;
            }
        }
        // メールアドレス・電話番号チェック
        $params['mail'] = trim($params['mail']);
        $params['tel'] = trim($params['tel']);
        if (empty($params['mail']) && empty($params['tel']))
        {
            // メールアドレス・電話番号の両方が未入力の場合
            $this->_data['renraku_empty_error'] = "error";
            $this->_data['renraku_error'] = "error";
            $error = 1;
        }
        else
        {
            if (!empty($params['mail']) && $this->checkMail($params['mail']))
            {
                // メールアドレスが入力されているかつ、入力形式が正しくない場合
                $this->_data['mail_error'] = "error";
                $this->_data['renraku_error'] = "error";
                $error = 1;
            }
            if (!empty($params['tel']) && !preg_match("/^[0-9]+$/u", $params['tel']))
            {
                // 電話番号が入力されているかつ、入力形式が正しくない場合
                $this->_data['tel_error'] = "error";
                $this->_data['renraku_error'] = "error";
                $error = 1;
            }
        }
        // お問い合わせ内容チェック
        $checks = 0;
        for ($i = 1; $i <= 4; $i++)
        {
            // チェックボックス４つ分をループ
            if ($params['check' . $i] == 'on')
            {
                // チェックボックスonがあれば、+1
                $checks++;
            }
        }
        if ($checks == 0)
        {
            // チェックボックスがすべてoffの場合
            $this->_data['checks_error'] = "error";
            $error = 1;
        }
        // if (strlen ( $params ['shosai'] ) > 500) {
        // // 自由記載欄の文字数が500文字以上の場合
        // $this->_data ['shosai_error'] = "error";
        // $error = 1;
        // }

        return $error;
    }

}
?>
